package com.itwn.portal.web;

import com.github.pagehelper.PageInfo;
import com.itwn.portal.entity.Buy;
import com.itwn.portal.entity.Section;
import com.itwn.portal.entity.User;
import com.itwn.portal.reult.ResponseResult;
import com.itwn.portal.service.HistoryService;
import com.itwn.portal.service.SectionService;
import com.itwn.portal.service.UserService;
import com.itwn.portal.vo.SectionReadVo;
import com.itwn.portal.vo.SectionVo;
import com.itwn.portal.vo.UserHistoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("section")
public class SectionController {
    @Autowired
    SectionService sectionService;
    @Autowired
    UserService userService;
    @Autowired
    HistoryService historyService;

    @GetMapping("/next")
    public ResponseResult<Integer> next(Integer rid,Integer sid){
        ResponseResult<Integer> result = new ResponseResult<Integer>();
        Integer id=sectionService.queryNext(rid,sid);
        result.setCode(200);
        result.setData(id);
        return result;
    }


    @GetMapping("/weekdaySectionList")
    public Map<Integer, List<SectionVo>> weekdaySectionList(Integer weekday) {
        Map<Integer, List<SectionVo>> map = sectionService.queryweekdaySectionList(weekday);
        return map;
    }

    @RequestMapping("/list")
    public ResponseResult<PageInfo<Section>> list(@RequestParam(value = "size", defaultValue = "4") Integer size,
                                                  @RequestParam(value = "now", defaultValue = "1") Integer now,
                                                  Integer id) {
        ResponseResult<PageInfo<Section>> result = new ResponseResult<>();
        try {
            PageInfo<Section> pageInfo = sectionService.querySectionByPage(size, now, id);
            if (pageInfo != null) {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            } else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }

    @GetMapping("/listLatest")
    @ResponseBody
    public List<Section> listLatest(Integer id) {
        return this.sectionService.queryLatestSection(id);
    }


    @GetMapping("/initPageList")
    public ResponseResult<SectionReadVo> initPageList(HttpServletRequest request, Integer uid, Integer rid, Integer sid) {
        ResponseResult<SectionReadVo> result = new ResponseResult<>();
        SectionReadVo sectionReadVo = sectionService.querySectionReadVo(rid, sid);
        HttpSession session = request.getSession();
        if (sectionReadVo == null) {
            result.setCode(201);
            result.setMsg("资源加载失败");
            return result;
        }
        //先看是不是免费 0不免费
        if (sectionReadVo.getIsFree() == 0) {
            //看是不是已经登录
            User loginUser = (User) session.getAttribute("loginUser");

            if (loginUser == null) {
                result.setCode(202);
                result.setMsg("阅读付费章节未登录");
                return result;
            }
            //查看是否vip
            Byte isvip = loginUser.getIsvip();
            if (isvip != 1) {
                System.out.println("不是vip");
                //查看是否已经购买
                Buy buy = userService.queryBuyByUidAndSid(loginUser.getId(), sid);
                if (buy == null) {
                    System.out.println("未购买");
                    //先扣阅点没有再扣点券
                    Boolean f = userService.readupdate(loginUser,sid);
                    System.out.println("扣费" + f);
                    if (f == false) {
                        result.setCode(203);
                        result.setMsg("账号余额不足");
                        return result;
                    }
                }
                System.out.println("已购买");
            }
        }
        //保存浏览记录
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            UserHistoryVo historyVo = new UserHistoryVo();
            historyVo.setUid(loginUser.getId());
            historyVo.setSid(sid);
            historyVo.setRid(rid);
            historyVo.setSname(sectionReadVo.getSname());
            historyVo.setRname(sectionReadVo.getRname());
            historyService.setHistory(historyVo);
            System.out.println("-----------------------"+historyVo);
        }
        //加载阅读页
        result.setCode(200);
        result.setMsg("验证通过");
        result.setData(sectionReadVo);
        return result;
    }

    /**
     * 查询上次观看章节
     * @param uid
     * @param rid
     * @return
     */
    @GetMapping("/getLastWatchSection")
    public ResponseResult<UserHistoryVo> getLastWatchSection(Integer uid,Integer rid){
        ResponseResult<UserHistoryVo> result = new ResponseResult<>();
        try{
            UserHistoryVo uhVo = this.historyService.getHistory(uid,rid);
            System.out.println("上次观看记录：：：：：："+uhVo);
            result.setCode(200);
            result.setMsg("查询上次观看章节成功!");
            result.setData(uhVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询上次观看章节失败!");
        }
        return result;
    }
}
